package org.lq.er.view.admin;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.lq.er.entity.Admin;
import org.lq.er.service.AdminService;
import org.lq.er.service.impl.AdminServiceImpl;
import org.lq.er.util.CastUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
/**
 * 	增删改查主界面
 * @author 六组刘明昕
 * @date 2020-8-29 0:20:09
 */
public class V_admin_aduq extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_admin_aduq frame = new V_admin_aduq();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_admin_aduq() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 508, 411);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 271, 461, 91);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btn_Query = new JButton("查询");
		btn_Query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				initTable();
				
			}
		});
		btn_Query.setBounds(54, 10, 93, 23);
		panel.add(btn_Query);
		
		JButton btn_Quit = new JButton("退出");
		btn_Quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btn_Quit.setBounds(276, 56, 93, 23);
		panel.add(btn_Quit);
		
//		JButton btn_Delete = new JButton("删除");
//		btn_Delete.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				int index = table.getSelectedRow();
//				if(index>=0) {
//					int n = JOptionPane.showConfirmDialog(V_admin_main.this, "确定删除"+table.getValueAt(index, 1)+"管理员吗?","删除",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
//					if(n == 0) {
//						int id = CastUtil.castInt(table.getValueAt(index, 0));
//						if(new AdminServiceImpl().delete(id)) {
//							JOptionPane.showMessageDialog(V_admin_main.this, "删除成功!");
//							initTable();
//						}else {
//							JOptionPane.showMessageDialog(V_admin_main.this, "删除失败!");
//						}
//					}
//				}else {
//					JOptionPane.showMessageDialog(V_admin_main.this,"请选择需要删除的管理员");
//				}
//				initTable();
//			}
//		});
//		btn_Delete.setBounds(117, 58, 93, 23);
//		panel.add(btn_Delete);
		
		JButton btn_Add = new JButton("添加");
		btn_Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_admin_add add = new V_admin_add();
				add.setVisible(true);
				initTable();
			}
		});
		btn_Add.setBounds(276, 10, 93, 23);
		panel.add(btn_Add);
		
		JButton btn_Add_1 = new JButton("修改");
		btn_Add_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int rowIndex = table.getSelectedRow();
				int id = Integer.parseInt(table.getValueAt(rowIndex, 0).toString());
				V_admin_update update = new V_admin_update(id);
				update.setVisible(true);
				update.addWindowListener(new WindowListener() {
					
					@Override
					public void windowOpened(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void windowIconified(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void windowDeiconified(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void windowDeactivated(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void windowClosing(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void windowClosed(WindowEvent e) {
						initTable();
						
					}
					
					@Override
					public void windowActivated(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});
		btn_Add_1.setBounds(54, 55, 93, 23);
		panel.add(btn_Add_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 20, 461, 241);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "\u59D3\u540D", "\u624B\u673A\u53F7", "\u8EAB\u4EFD\u8BC1\u53F7"
			}
		));
		scrollPane.setViewportView(table);
		initTable();
	}
	//展示信息
	public void initTable() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for(int i = 0;i< count;i++) {
			dtm.removeRow(0);
		}
		AdminService as = new AdminServiceImpl();
		List<Admin> all = as.findAll();
		for (Admin admin : all) {
			dtm.addRow(new Object[] {
					admin.getId(),
					admin.getName(),
					admin.getPhone_no(),
					admin.getCardId()
			});
		}
	}
}
